import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    name: 'login',
    path: '/login',
    component: () => import('../views/UserLogin.vue')
  },
  {
    name: 'register',
    path: '/register',
    component: () => import('../views/UserResgiser.vue')
  },
  {
    path: '/',
    redirect: '/home',
    component: () => import('../views/MainView.vue'),
    children: [
      /**
       * 通用路由
       * **/
      {
        name: 'home',
        path: '/home',
        component: () => import('../views/HomeView.vue')
      },
      {
        name: 'userinfo',
        path: '/userinfo',
        component: () => import('../views/UserInfo.vue')
      },

      /**
       * 用户路由
       * **/

      {
        name: 'knowledgebase-user',
        path: '/knowledgebase-user',
        component: () => import('../views/user/KnowledgeBase.vue')
      },
      {
        name: 'expertManagement-user',
        path: '/expertManagement-user',
        component: () => import('../views/user/ExpertInfo.vue')
      },
      {
        name: 'userfeedback-user',
        path: '/userfeedback-user',
        component: () => import('../views/user/UserFeedback.vue')
      },
      {
        name: 'diagnosticRecord-user',
        path: '/diagnosticRecord-user',
        component: () => import('../views/user/DiagnosticRecord.vue')
      },
      {
        name: 'intelligent-search',
        path: '/intelligent-search',
        component: () => import('../views/user/IntelligentSearch.vue')
      },
      {
        name: 'expert-consultation',
        path: '/expert-consultation',
        component: () => import('../views/user/ExpertConsultation.vue')
      },

      /**
       * 专家路由
       * **/
      {
        name: 'knowledgebase-expert',
        path: '/knowledgebase-expert',
        component: () => import('../views/expert/KnowledgeBase.vue')
      },

      /**
       * 管理员路由
       * **/
      {
        name: 'userinfomanagement',
        path: '/userinfomanagement',
        component: () => import('../views/Administrator/UserinfoManagement.vue')
      },
      {
        name: 'expertmanagement',
        path: '/expertmanagement',
        component: () => import('../views/Administrator/ExpertManagement.vue')
      },
      {
        name: 'knowledgebasemanagement',
        path: '/knowledgebasemanagement',
        component: () => import('../views/Administrator/KnowledgeBase.vue')
      },
      {
        name: 'userfeedback',
        path: '/userfeedback',
        component: () => import('../views/Administrator/UserFeedback.vue')
      },
      {
        name: 'diagnosticrecord',
        path: '/diagnosticrecord',
        component: () => import('../views/Administrator/DiagnosticStatistics.vue')
      }
    ]
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  //如果路径是'/login'可以直接放行
  if (to.path === '/login' || to.path === '/register') {
    return next()
  }
  //获取缓存的token

  // console.log(userinfo)
  //若是没有token则跳转到注册页面,若是存在token则直接放行

  setTimeout(() => {
    let userinfo = JSON.parse(localStorage.getItem('userinfo'))
    if (!userinfo) {
      return next({ name: 'login' })
    } else {
      next()
    }
  }, 0)
})

// //解决点击同一个路由报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

//导出路由
export default router
